package com.ghck.server.bkcontroller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ghck.server.exception.GhckException;
import com.ghck.server.aop.PathAndMsgOnError;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.model.DiseaseCategoryDict;
import com.ghck.server.model.SysUser;
import com.ghck.server.model.TestCategoryDict;
import com.ghck.server.model.Users;
import com.ghck.server.model.query.TestAnswerQuery;
import com.ghck.server.model.query.TestCategoryDictQuery;
import com.ghck.server.model.response.Result;
import com.ghck.server.model.response.ResultList;
import com.ghck.server.model.response.TestAnswerRes;
import com.ghck.server.service.DiseaseCategoryDictService;
import com.ghck.server.service.TestAnswerItemService;
import com.ghck.server.service.TestAnswerService;
import com.ghck.server.service.TestCategoryDictService;
import com.ghck.server.service.UsersService;

/**
 * 
 * @author chenxuan
 *
 */
@Controller
@RequestMapping(value="/system")
public class SystemController {

	@Autowired
	private UsersService usersService;
	@Autowired
	private DiseaseCategoryDictService diseaseCategoryDictService;
	@Autowired
	private TestAnswerService testAnswerService;
	@Autowired
	private TestCategoryDictService testCategoryDictService;
	/**
	 * 管理平台首页
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/")
	public String login(HttpServletRequest request){
		
		return "back/login";
	}
	
	@RequestMapping(value="/index")
	public String index(HttpServletRequest request){
		Object obj = request.getSession().getAttribute("user");
		if(obj == null){
			return "back/login";
		}
		return "back/index";
	}
	/**
	 * 登录 
	 * @param user
	 * @param model
	 * @param request
	 * @return
	 */
	@PathAndMsgOnError(path="/back/login",errorMsgField="errorMsg")
	@RequestMapping(value="/login")
	public String login(Users user,Model model,HttpServletRequest request){
		Object obj = request.getSession().getAttribute("systemName");
		if(null == obj && user.getMobile() == null){
			return "back/login";
		}
		Users su = usersService.sysUserLogin(user);
		request.getSession().setAttribute("systemName", "工和创客管理平台");
		request.getSession().setAttribute("user", su);
		return "redirect:/system/index";
	}
	/**
	 * 登出
	 * @param sysUser
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/logout")
	public String logout(SysUser sysUser,Model model,HttpServletRequest request){
		request.getSession().removeAttribute("systemName");
		request.getSession().removeAttribute("user");
		return "back/login";
	}
	/**
	 * 疾病分类管理
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/disease/category/manager")
	public String diseaseCategoryManager(Model model,DiseaseCategoryDict dcd ,HttpServletRequest request){
		List<DiseaseCategoryDict> diseaseCategoryDictList = diseaseCategoryDictService.findAllByLevelOne();
		if(null != dcd.getId()){
			DiseaseCategoryDict diseaseCategoryDict = diseaseCategoryDictService.findDiseaseCategoryDictById(dcd.getId());
			model.addAttribute("diseaseCategoryDict", diseaseCategoryDict);
			List<DiseaseCategoryDict> subDiseaseCategoryDict = diseaseCategoryDictService.findAllByByParentId(dcd.getId());
			model.addAttribute("subDiseaseCategoryDict", subDiseaseCategoryDict);
		}
		model.addAttribute("diseaseCategoryDictList", diseaseCategoryDictList);
		return "back/disease/category_manager";
	}
	/**
	 * 疾病分类 -- 添加分类
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/disease/category/merge")
	@ResponseBody
	public Result diseaseCategoryMerge(Model model,DiseaseCategoryDict dcd ,HttpServletRequest request){
		if(dcd.getName() == null || dcd.getName().trim().isEmpty()){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "症状名称不能为空");
		}
		if(dcd.getParentId() == null || dcd.getParentId() == 0){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "父ID不能为空");
		}
		if(dcd.getId() == null){
			dcd.setLevel(2);
			diseaseCategoryDictService.insertDiseaseCategoryDict(dcd);
		}else{
			dcd.setLevel(2);
			diseaseCategoryDictService.updateDiseaseCategoryDict(dcd);
		}
		
		return new Result();
	}
	
	/**
	 * 疾病分类 -- 删除
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/disease/category/delete")
	@ResponseBody
	public Result diseaseCategoryDelete(Model model,DiseaseCategoryDict dcd ,HttpServletRequest request){
		if(dcd.getId() == null || dcd.getId() == 0){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "症状ID不能为空");
		}
		diseaseCategoryDictService.deleteDiseaseCategoryDictById(dcd.getId());
		return new Result();
	}
	
	/**
	 * 疾病搜索热词管理 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/disease/keyword/manager")
	public String keywordManager(Model model,HttpServletRequest request){
		
		return "back/disease/keyword_manager";
	}
	
	/**
	 * 健康自测管理
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/test/manager")
	public String testManager(Model model,TestCategoryDictQuery query,HttpServletRequest request){
		query.setParentId(0);
		List<TestCategoryDict> testCategoryDictList = testCategoryDictService.findTestCategoryDictByQuery(query);
		model.addAttribute("testCategoryDictList", testCategoryDictList);
		return "back/test/test_manager";
	}
	
	/**
	 * 健康自测管理
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/test/category_dict/query")
	@ResponseBody
	public ResultList<TestCategoryDict> categoryDictQuery(Model model,TestCategoryDictQuery query,HttpServletRequest request){
		ResultList<TestCategoryDict> list = new ResultList<TestCategoryDict>();
		List<TestCategoryDict> testCategoryDictList = testCategoryDictService.findTestCategoryDictByQuery(query);
		list.setItemList(testCategoryDictList);
		return list;
	}
	/**
	 * 健康自测管理测试题列表
	 * @param model
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/test/list")
	public String testList(Model model,TestAnswerQuery query,HttpServletRequest request){
		List<TestAnswerRes> testAnswerList = testAnswerService.findTestAnswerByQuery(query);
		model.addAttribute("testAnswerList", testAnswerList);
		return "back/test/test_list";
	}
}